package com.mo.demo;

import static android.app.WallpaperManager.FLAG_SYSTEM;

import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.home.base.LedBleActivity;
import com.home.base.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class TestActivity extends LedBleActivity {

    Test test;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setWall();
        Enjoy.setScreen(this, 0);
        Enjoy.keepAlive(this);

        test = new Test(this);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Enjoy.showNavi();
            }
        }, 1000);

//        new Thread() {
//            public void run() {
//                changeName();
//            }
//        }.start();

        addFragment(R.id.frame, new DrawFragment());
    }

    public void addFragment(int id, Fragment fragment) {
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(id, fragment);
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private void changeName() {
        File f = new File(Environment.getExternalStorageDirectory(), "out");
        ArrayList<File> list = new ArrayList<>();
        for (File tmp : f.listFiles()) {
            list.add(tmp);
        }
        Collections.sort(list, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (int i = 0; i < list.size(); i++) {
            File src = list.get(i);
            src.renameTo(new File(src.getParentFile(), "q" + i + ".png"));
            src.delete();
        }
        new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(this, "finish", Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (test != null) {
            test.onDestroy();
        }
    }

    public static void setWall() {
        try {
            WallpaperManager wpm = (WallpaperManager) MyApp.getApp().getSystemService(Context.WALLPAPER_SERVICE);
            if (wpm != null) {
                Bitmap mBitmap = BitmapFactory.decodeResource(MyApp.getApp().getResources(), R.raw.wall);
                //第一个参数是Bitmap对象，第二个参数是截取图片的大小矩形，第三个参数是是否备份
                wpm.setBitmap(mBitmap, null, true, FLAG_SYSTEM);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public void notifyAllActivity(String str) {
        System.currentTimeMillis();
    }

    @Override
    public void notifyReceiveData(String str, String str2) {
        System.currentTimeMillis();
    }
}
